#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <math.h>
#include <string>
#include <iterator>
#include <fstream> 

const long MAX_TIME             = 10000;
const long NO_STATIONS          = 100;
const long FRAME_LENGTH         = 100;
const long TOTAL_NO_RUNS        = 10000;
      long collisions           = 0;
      long medium_occupied_till = -1;
      long survival_timer       = -1;
      long successful_packets   = 0;

class Station
{
public:
  long send_till;

public:
  void Init();
  void TriggerSend(long);

}; // class Station

void Station::Init()
{
  send_till = -1;
} // Station::Init

void Station::TriggerSend(long current_time)
{
  if(current_time < medium_occupied_till) {
    collisions++;
    survival_timer = -1;
  } // if
  else survival_timer = 0;

  medium_occupied_till = current_time + FRAME_LENGTH;
} // Station::TriggerSend

main()
{
  Station* station          = new Station[NO_STATIONS];

  for(long i = 0; i < NO_STATIONS; i++)
    station[i].Init();

  for(long arrival_rate = 0; arrival_rate < TOTAL_NO_RUNS/(NO_STATIONS/4); arrival_rate += 1) {

    cerr << arrival_rate*100/TOTAL_NO_RUNS << " %              " << (char)(13) << flush;

    collisions           = 0;
    medium_occupied_till = -1;
    survival_timer       = -1;
    successful_packets   = 0;

    for(long time = 0; time < MAX_TIME; time++) {

      if((time % FRAME_LENGTH) == 0) 
	for(long station_index = 0; station_index < NO_STATIONS; station_index++)	
	  if((abs(rand()) % (TOTAL_NO_RUNS)) < arrival_rate) station[station_index].TriggerSend(time);
      
      if(survival_timer != -1) survival_timer++;
      if(survival_timer == FRAME_LENGTH) {
	successful_packets++;
	survival_timer = -1;
      } // if
    } // for

    long no_frames = MAX_TIME / FRAME_LENGTH;
    double overall_arrival_rate = ((double)NO_STATIONS*(double)arrival_rate)/(double)(TOTAL_NO_RUNS);

    if(true) {
      cout << overall_arrival_rate << " " << (double)successful_packets/(double)no_frames << endl; 
    } // fi
    else {
     // cout << "Arrival rate:          " << overall_arrival_rate << endl;
      //cout << "Successful packets:    " << successful_packets << endl;
    //  cout << "Number of frame-times: " << no_frames << endl;
     // cout << "Collisions occurred:   " << collisions << endl << endl;
    } // else
  } // for

  delete[] station;
} // main
